// 引入接口文件
import { queryRecommendAndDealNum,exportList, getListBy} from './intermediaryListServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download } from 'utils/util'


// 初始默认状态
const defultState = {
  exportLoading: false,
  loading: false,
  btnLoading: false,
  statisticsInfo:{
    dealNum: 0,//成交数
    recommendNum: 0,//报备数
  },//统计信息
  biz:{
    queryForm:{}
  },
 // 表格数据
 tableData: {
  dataSource: [
  ],
  total: 0,
  stop:0,
  start:0
  },
  // // 表格分页设置
  // pageConfig: {
  //   pageSize: Number.parseInt(config.smallPageSizeList[0]),
  //   pageNo: 1
  // },
  visible:false,
  orgId:''
}

// 声明module
const tmpModule = {
  // namespace: 'productListModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
          // 执行业务操作
          dispatch({type: 'search',payload: {} }) // end dispatch
          dispatch({type: 'queryRecommendAndDealNum',payload: {} }) // 查询统计信息
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    // 详情
    *queryRecommendAndDealNum({payload }, { put,select,call}) {
      let { queryList ,statisticsInfo} = yield select(state => state[tmpModule.namespace])
      const {success,msg,data} = yield call(queryRecommendAndDealNum);
      statisticsInfo=data||{}
      yield put({ type: 'updateStore', payload: { statisticsInfo}});
    },
    
    //导出
    * exportList ({payload}, {put, call, select}) {
      let {queryList,exportLoading,biz:{queryForm}} = yield select(state => state[tmpModule.namespace])
      //组装接口入参
      let params = {
        // orgId, agentConfigId
        ...queryForm,
      }
      yield put({type: 'updateStore', payload: {exportLoading:true}})
      let {data = '', success, msg} = yield call(exportList, {...params})
      yield put({type: 'updateStore', payload: {exportLoading:false}})
      
      if (!success) {
        message.error(msg)
        return false
      }
      if (data) {
        download(data)
      }
    
    },
    *getListBy({payload},{ put,select,call}){
      console.log(payload.orgId)
      try{
        let {rows = {}, success, msg} = yield call(getListBy, payload.orgId)
        let { tableData} = yield select(state => state[tmpModule.namespace])
        if(success){
          tableData.dataSource =rows,
          yield put({type: 'updateStore', payload: {tableData}})
        }
      }catch(e){
        console.log(e)
      }
      
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
